package edu.nju.desserthouse.service;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import nju.edu.desserthouse.model.User;

import edu.nju.desserthouse.dao.UserDao;
import edu.nju.desserthouse.exceptions.PasswordNotMatchException;
import edu.nju.desserthouse.exceptions.UsernameExistException;

public class UserService implements UserServiceInterface
{
	
	@EJB
	UserDao userDao;
	
	private static UserService userService=new UserService();
	
	private UserService()
	{
		
	}
	
	public static UserService getInstance()
	{
		return userService;
	}
	
	public User validateUser(String userid,String password)
	{
		User user=userDao.find("userid",userid);
		if(user==null)
		{
			return null;
		}
		else if(!user.getPassword().equals(password))
		{
			return null;
		}
		
		return user;
	}

	public String validateRegister(User user,String message) throws PasswordNotMatchException ,UsernameExistException
	{
		if(userDao.find("userid",user.getUserid())!=null)
		{
			throw new UsernameExistException();
		}
		message=validateUpdateUser(user,message);
		return message;

	}
	
	public String validateUpdateUser(User user,String message) throws PasswordNotMatchException
	{
		if(user.getBankid()==""||user.getEmail()==""||user.getName()==""||user.getPassword()==""||user.getPassword()==""||user.getPhone()==""||user.getUserid()==""||user.getBankid()==null||user.getEmail()==null||user.getName()==null||user.getPassword()==null||user.getPassword()==null||user.getPhone()==null||user.getUserid()==null)
		{
			message+="All the content must be filled!<br>";
		}
		
		if(!user.getPassword().equals(user.getPassword()))
		{
			throw new PasswordNotMatchException();
		}
		
		return message;
	}
	
	public Integer validateNumber(String number)
	{
		Integer num=null;
		try
		{
			num=Integer.valueOf(number);
		}
		catch(Exception e)
		{
			return null;
		}
		
		if(num.intValue()<=0)
		{
			return null;
		}
		
		return num;
	}
	
	public void sentErrorMessage(String message,HttpServletRequest req,HttpServletResponse resp) 
					throws ServletException,IOException
	{
		req.setAttribute("message",message);
		RequestDispatcher dispater=req.getRequestDispatcher(resp.encodeURL("/error/error.jsp"));
		dispater.forward(req,resp);
	}
	

	public void sentMessage(String message,HttpServletRequest req,HttpServletResponse resp) 
					throws ServletException,IOException
	{
		req.setAttribute("message",message);
		RequestDispatcher dispater=req.getRequestDispatcher(resp.encodeURL("/message/message.jsp"));
		dispater.forward(req,resp);
	}
	

	public void forwardPage(String page,HttpServletRequest req,HttpServletResponse resp) 
					throws ServletException,IOException
	{
		RequestDispatcher dispater=req.getRequestDispatcher(resp.encodeURL(page));
		dispater.forward(req,resp);
	}

}
